//index.js
//获取应用实例
var common = require('../../common.js');
const app = getApp()
var calltag = 'true';
var rsdata = [];
var initdata = 'Please say some request'
var aftsvar = '正在解析您的指令'
var fp;

Page({
	data: {
		motto: 'By VMCloud StatLee',
		echors: initdata,
		userInfo: {},
		hasUserInfo: false,
		canIUse: wx.canIUse('button.open-type.getUserInfo')
	},
	//事件处理函数
	bindViewTap: function () {
		wx.navigateTo({
			url: '../logs/logs'
		})
	},
	onLoad: function () {
		if (app.globalData.userInfo) {
			this.setData({
				userInfo: app.globalData.userInfo,
				hasUserInfo: true
			})
		} else if (this.data.canIUse) {
			// 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
			// 所以此处加入 callback 以防止这种情况
			app.userInfoReadyCallback = res => {
				this.setData({
					userInfo: res.userInfo,
					hasUserInfo: true
				})
			}
		} else {
			// 在没有 open-type=getUserInfo 版本的兼容处理
			wx.getUserInfo({
				success: res => {
					app.globalData.userInfo = res.userInfo
					this.setData({
						userInfo: res.userInfo,
						hasUserInfo: true
					})
				}
			})
		}

		/* wx.connectSocket({
			url: common.socketurl,
			header: {
				'content-type': 'application/json'
			},
			method: "POST"
		})

		wx.onSocketOpen(function (res) {
			console.log('WebSocket连接已打开！')
		})

		wx.onSocketMessage(function (res) {
			console.log('收到服务器内容：' + res.data)
		}) */
	},
	getUserInfo: function (e) {
		console.log(e)
		app.globalData.userInfo = e.detail.userInfo
		this.setData({
			userInfo: e.detail.userInfo,
			hasUserInfo: true
		})
	},
	callvarvis: function () {
		var that = this;
		const recorderManager = wx.getRecorderManager()
		const options = {
			duration: 60000,
			sampleRate: 16000,
			numberOfChannels: 1,
			encodeBitRate: 24000,
			format: 'mp3',
			frameSize: 2048
		}
		wx.setStorageSync('hasRecord', "1");
		recorderManager.start(options);
	},
	endvarvis: function () {
		var that = this;
		const recorderManager = wx.getRecorderManager();
		recorderManager.stop();
		recorderManager.onStop((res) => {
			const tempFilePath = res
			console.log(res);
			that.setData({
				recordFile: res.tempFilePath
			})
			//**重点重点**：录音完成后直接创建 播放  this.***为全局变量，可以在其他方法中调用
			this.innerAudioContext = wx.createInnerAudioContext()
			this.innerAudioContext.src = that.data.recordFile;
		})
		recorderManager.onError((res) => {
			console.log(res);
			console.log("录音错误");
			calltag = 'false'
			aftsvar = '指令错误'
		})
		recorderManager.onFrameRecorded((res) => {
			console.log(res);
			const {
				frameBuffer
			} = res
			// console.log('frameBuffer.byteLength', frameBuffer.byteLength)
			// console.log('frameBuffer', frameBuffer)
			that.setData({
				recordData: frameBuffer
			})
		})
	},
	connectAI: function (data) {
		/* wx.request({
			url: common.apiurl, //仅为示例，并非真实的接口地址
			method: 'POST',
			dataType: 'json',
			data: {
				'sfp': fp,
			},
			header: {
				'content-type': 'application/json' // 默认值
			},
			success: function (res) {
				console.log(res.data);
			}
		}) */
		/* wx.sendSocketMessage({
			data: {
				'sfp' : '123',
			},
		}) */
		wx.uploadFile({
			url: common.apiurl, //仅为示例，非真实的接口地址
			filePath: fp,
			name: 'sfp',
			success: function (res) {
				var data = res.data
				console.log(res);
				//do something
			}
		})
	},
	saveFile: function () {
		wx.saveFile({
			tempFilePath: this.innerAudioContext.src,
			success: function (res) {
				fp = res.savedFilePath;
				console.log(fp);
				wx.uploadFile({
					url: 'https://upgocobh.qcloud.la/upload', 
					filePath: fp,
					name: 'file',
					formData: {
						　　'msg': 'voice'
					}, 
					success: function(res){
						console.log(res.data);		  
					}
				  })
			}
		})

	},
	playFile: function () {
		const innerAudioContext = wx.createInnerAudioContext()
		innerAudioContext.autoplay = true
		innerAudioContext.src = fp
		innerAudioContext.onPlay(() => {
			console.log('开始播放')
			aftsvar = '完成解析'
		})
		innerAudioContext.onError((res) => {
			console.log(res.errMsg)
			console.log(res.errCode)
			aftsvar = '解析失败'
		})
		innerAudioContext.onEnded(() => {
			rsdata.push(aftsvar)
			this.setData({
				echors: initdata + '\n' + rsdata.join('\n')
			})
		})
	},
	addrs: function () {

		if (calltag == 'true') {
			rsdata.push(aftsvar)
			this.setData({
				echors: initdata + '\n' + rsdata.join('\n')
			})
		}
		if (calltag == 'false') {
			this.setData({
				echors: "对不起，我没有听清"
			})
		}
	},
	playvoicebu: function () {
		const innerAudioContext = wx.createInnerAudioContext()
		innerAudioContext.autoplay = true
		innerAudioContext.src = this.innerAudioContext.src
		innerAudioContext.onPlay(() => {
			console.log('开始播放')
			aftsvar = '完成解析'
		})
		innerAudioContext.onError((res) => {
			console.log(res.errMsg)
			console.log(res.errCode)
			aftsvar = '解析失败'
		})
		innerAudioContext.onEnded(() => {
			rsdata.push(aftsvar)
			this.setData({
				echors: initdata + '\n' + rsdata.join('\n')
			})
		})

	}
})